package edu.ccut.saturn.authority.impl.department;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IAuthorityResourceInfo;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnAuthorityManager;

public class QueryBundleList extends DefaultComponent {
	@Override
	public void execute(IParameterContext parameterContext) throws Exception {
		String bundleName = "";
		try {
			bundleName = parameterContext.getParameterValue(0).toString();
		} catch (NullPointerException e) {
			bundleName = "";
		}
		int count = 0;
		List<IAuthorityResourceInfo> resourceList = SaturnAuthorityManager
				.getInstance().getAllResources();
		List<Map<String, String>> bundleResurce;
		if (bundleName != null && !"".equals(bundleName)) {
			List<IAuthorityResourceInfo> bundleRsourceList = SaturnAuthorityManager
					.getInstance().getSpecieledBundleResources(bundleName);
			
			if (bundleRsourceList != null) {
				count = bundleRsourceList.size();
			}
			bundleResurce = infoToMap(bundleRsourceList);
			
		} else {
			if (resourceList != null) {
				count = resourceList.size();
			}
			bundleResurce = infoToMap(resourceList);
		}
		parameterContext.setParameter(1, bundleResurce);
		parameterContext.setParameter(2, count);
	}

	@SuppressWarnings("unused")
	private List<Map<String, String>> infoToMap(
			List<IAuthorityResourceInfo> info) {
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();
		if(info != null) {
			for (IAuthorityResourceInfo key : info) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("resourceKey", key.getResourceKey());
				map.put("name", key.getName());
				map.put("description", key.getDescription());
				list.add(map);
			}
		}
		return list;
	}
}